﻿using AntDesign;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.CodeAnalysis.Differencing;
using SageKing.Studio.Pages.Base;
using SageKing.Studio.Pages.Template;
using SageKing.Studio.Shared;
using System.Drawing.Drawing2D;

namespace SageKing.Studio.Pages.SageKingMessage.component;

public class SysSageKingMessageEditModal(
    IMessageService ShowMessage,
    IConfirmService _confirmService,
    SysSageKingMessageService dataService
    )

    : ModalCRUDBase<SysSageKingMessage, SysSageKingMessageEdit, long, SysSageKingMessage>
{

    public PageBase<SysSageKingMessage> PageData { get; set; }

    public Action ResetDataAction { get; set; } = () => { };
    public Action StateHasChanged { get; set; } = () => { };
    public Func<bool, Task> RefreshFunc { get; set; } = (a) => Task.CompletedTask;

    #region IModalCRUD
    public IModalCRUD<SysSageKingMessage, SysSageKingMessageEdit, long, SysSageKingMessage> MeCrud => this;

    public string TabTitle { get; set; }
    public string ParentName { get; set; }
    public string Title { get; set; } = "消息结构新增";

    public string OkTitle { get; set; } = "保存";

    public string CancelText { get; set; } = "取消";

    public bool Visible { get; set; } = false;

    public bool Loading { get; set; } = false;

    public SysSageKingMessage Model { get; set; }
    public SysSageKingMessageEdit Component { get; set; }
    public Error? Error { get; set; }
    public RenderFragment ModalTemplate { get; set; }

    public async Task DeleteData(SysSageKingMessage row, SysSageKingMessage parent = null)
    {
        PageData.Items = PageData.Items.Where(a => a.Id != row.Id);
        PageData.Total -= 1;
        ResetDataAction();
        await dataService.Delete(row.Id);
        _ = ShowMessage.SuccessAsync($"删除【{row.Name}】成功", 2);
    }

    public async Task EditOnStatusSwitch(SysSageKingMessage entity)
    {
        try
        {
            var id = entity.Id;
            var state = entity.Status;
            string content = !state ? $"确认关闭【{entity.Name}】" : $"确认开启【{entity.Name}】";

            var confirmResult = await _confirmService.Show(content + "?", "提示", ConfirmButtons.OKCancel);
            if (confirmResult == ConfirmResult.OK)
            {
                Expression<Func<SysSageKingMessage, bool>> wherefunc = a => a.Id == id;
                Expression<Func<SysSageKingMessage, object>> updatefunc = a => new { a.Status };

                await dataService.UpdateWithOptLock(entity, updatefunc);
                _ = ShowMessage.SuccessAsync($"{content} 成功", 2);
            }
            else
            {
                entity.Status = !state;
                StateHasChanged();
            }
        }
        catch (Exception ex)
        {
            Error?.ProcessError(ex);
        }
    }

    public async Task HandleOk(MouseEventArgs e)
    {
        try
        { 
            Loading = true;
            if (Component != null)
            {
                var result = await Component.Me.Save();
                if (!result)
                {
                    Visible = true;
                    Loading = false;
                    return;
                }
            }
            await RefreshFunc(false);
            _ = ShowMessage.SuccessAsync($"{Title}【{Model.Name}】成功！");
            Visible = false;
            Loading = false;
        }
        catch (Exception ex)
        {
            Error?.ProcessError(ex, $"{Title}【{Model.Name}】失败！");
        }
    }

    public Task<bool> InitModel(long id)
    {
        throw new NotImplementedException();
    }

    #endregion
}
